<template>
	<view>
		<van-toast id="van-toast" />
		<van-dialog id="van-dialog" :confirm-button-color="primaryColor"/>
		<view class="logo-title">
			<image class="logo" src="../../static/logo.png"></image>
			<view>公务智行</view>
		</view>

		<view class="wechat-login">
			<van-button :color="primaryColor" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" custom-class="login-custom-class"
			 :round="true">微信手机号快捷登录</van-button>
		</view>
	</view>
</template>

<script>
	import Toast from '@/wxcomponents/toast/toast'
	import Dialog from '@/wxcomponents/dialog/dialog';
	import utils from '@/common/utils.js'
	export default {
		data() {
			return {
				primaryColor: getApp().globalData.primaryColor,
				wxcode:''
			};
		},
		onLoad(options) {
			if(options.msg){
				Toast(options.msg)
			}
		},
		onShow() {
			const _this = this
			this.getWxcode()
		},
		methods: {
			getWxcode(){
				const _this = this
				uni.login({
					success(loginObj) {
						_this.wxcode = loginObj.code
						console.log(`code:${loginObj.code}`)
					}
				})
			},
			async getPhoneNumber(e) {
				if(e.detail.errMsg === 'getPhoneNumber:ok'){
					console.log(e.detail.errMsg)
					console.log(e.detail.iv)
					console.log(e.detail.encryptedData)
					let reqData = {}
					try{
						const code = this.wxcode
						console.log("req code:" + code)
						reqData = {
							wxcode: code,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv
						}
						const rspData = await utils.req('/wxuser/login', reqData)
						uni.setStorageSync('userinfo', rspData)
						uni.switchTab({
							url:'../index/index'
						})
					}catch(e){
						if(e.code == 205){//未审核
							Dialog.alert({
							  title: '提示',
							  message: '已提交审核，审核通过后方可继续操作'
							}).then(() => {
							  this.getWxcode()
							});
						}else if(e.code == 206){//不存在用户
							// uni.navigateTo({
							// 	url:`../user-binding/user-binding?tel=${e.data}&oper=newuser`
							// })
							Toast(e.msg)
						}else if(e.code == 207){//审核失败
							uni.navigateTo({
								url:`../user-binding/user-binding?tel=${e.data.tel}&oper=checkfail`
							})
						}else if(e.code == 208){//需要确认
							// const enData = encodeURIComponent(JSON.stringify(reqData))
							console.log(e.data.userId)
							uni.navigateTo({
								url:`../user-detail-confirm/user-detail-confirm?userId=${e.data.userId}&oper=needconfirm`
							})
						}else if(e.code == 301){//需要确认
							// const enData = encodeURIComponent(JSON.stringify(reqData))
							console.log(e.data)
							uni.navigateTo({
								url:`../user-detail-confirm/user-detail-confirm?tel=${e.data.tel}&oper=needconfirm`
							})
						}
						// this.getWxcode()
					}
				}else{
					Dialog.confirm({
					  title: '授权失败',
					  message: '你拒绝微信手机号授权登录，请使用其他手机号登录',
					  confirmButtonText: '其他手机号登录',
					  cancelButtonText: '我知道了'
					}).then(() => {
					  this.onOtherLogin()
					}).catch(() => {
					  // on cancel
					});
				}
				
			},
			wxLogin(code){
				
			},
			onOtherLogin(){
				uni.navigateTo({
					url:'../login-other/login-other'
				})
			}
		}
	}
</script>

<style lang="scss">
	.logo-title {
		margin-top: 100rpx;
		display: flex;
		flex-flow: column nowrap;
		align-items: center;

		.logo {
			border-radius: 100rpx;
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 30rpx;
			background-color: #F0AD4E;
		}
	}

	.wechat-login {
		display: flex;
		flex-flow: column;
		align-items: center;
		margin: 300rpx 100rpx 0 100rpx;
	}

	.login-custom-class {
		width: 500rpx;
	}
	.other-login{
		display: flex;
		flex-flow: column;
		align-items: center;
		margin-top: 100rpx;
		view:nth-child(2){
			color: $uni-color-primary;
			font-size: 2em;
		}
	}
</style>
